package com.syni.mdd.yhd.common.all.service.impl.business;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.controller.pay.aliPay.AliPayController;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsBusinessInfoDao;
import com.syni.mdd.yhd.common.all.dao.jpa.business.BmsBusinessAreaDao;
import com.syni.mdd.yhd.common.all.dto.reponseDto.business.BusinessAreaDto;
import com.syni.mdd.yhd.common.all.dto.requestDto.pay.PayReDto;
import com.syni.mdd.yhd.common.all.entity.BmsBusiness;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessInfo;
import com.syni.mdd.yhd.common.all.entity.BmsDxNews;
import com.syni.mdd.yhd.common.all.entity.business.BmsBusinessArea;
import com.syni.mdd.yhd.common.all.entity.business.BmsBusinessAreaRelation;
import com.syni.mdd.yhd.common.all.service.*;
import com.syni.mdd.yhd.common.all.service.business.BmsBusinessAreaRelationService;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import com.syni.mdd.yhd.common.all.utils.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.geom.Point2D;
import java.awt.print.Pageable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @className
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Log4j2
@Service
public class BmsBusinessAreaServiceImpl extends BaseService<BmsBusinessArea, Integer> implements BmsBusinessAreaService {

    @Autowired
    BmsBusinessAreaDao bmsBusinessAreaDao;

    @Autowired
    BmsBusinessService bmsBusinessService;

    @Autowired
    BmsBusinessAreaRelationService bmsBusinessAreaRelationService;

    @Override
    public BmsBusinessAreaDao getBaseDao() {
        return bmsBusinessAreaDao;
    }

    @Override
    public Map<String, Object> findAllArea(ParamsMap paramsMap) {
        String addrCity = paramsMap.getString("addrCity");
        if(StringUtils.isBlank(addrCity)) {
            addrCity = "珠海市";
        }
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("isDelete",Constants.COMMON_FLAG_FALSE_INT);
        conditionMap.put("addrCity",addrCity);
        Map<String,BusinessAreaDto> bmsBusinessAreaMap = new LinkedHashMap<>();
        List<BmsBusinessArea> bmsBusinessAreas = bmsBusinessAreaDao.findAll(new SpecificationUtils(conditionMap));
        List<BmsBusinessArea> genBmsBusinessArea = new LinkedList<>();
        if(bmsBusinessAreas != null && bmsBusinessAreas.size() > 0){
            for(BmsBusinessArea businessArea : bmsBusinessAreas){
                BmsBusinessArea bmsBusinessArea = new BmsBusinessArea();
                BeanUtils.copyProperties(businessArea,bmsBusinessArea);
                if(bmsBusinessArea.getIsHot() == Constants.COMMON_FLAG_TRUE_INT){
                    bmsBusinessArea.setAddrDistrict("热门商圈");
                    if(!bmsBusinessAreaMap.containsKey(bmsBusinessArea.getAddrDistrict())){
                        bmsBusinessAreaMap.put(bmsBusinessArea.getAddrDistrict(),new BusinessAreaDto(bmsBusinessArea.getAddrCity(),bmsBusinessArea.getAddrDistrict()));
                    }
                    bmsBusinessAreaMap.get(bmsBusinessArea.getAddrDistrict()).getBmsBusinessAreas().add(bmsBusinessArea);
                }else {
                    genBmsBusinessArea.add(bmsBusinessArea);
                }
            }
            if(genBmsBusinessArea != null && genBmsBusinessArea.size() > 0){
                for(BmsBusinessArea bmsBusinessArea : genBmsBusinessArea){
                    if(!bmsBusinessAreaMap.containsKey(bmsBusinessArea.getAddrDistrict())){
                        bmsBusinessAreaMap.put(bmsBusinessArea.getAddrDistrict(),new BusinessAreaDto(bmsBusinessArea.getAddrCity(),bmsBusinessArea.getAddrDistrict()));
                    }
                    bmsBusinessAreaMap.get(bmsBusinessArea.getAddrDistrict()).getBmsBusinessAreas().add(bmsBusinessArea);
                }
            }

        }
        List<BusinessAreaDto> businessAreaDtos = new LinkedList<>();
        if(bmsBusinessAreaMap.size() > 0){
            for(Map.Entry<String,BusinessAreaDto> businessAreaDtoEntry : bmsBusinessAreaMap.entrySet()){
                businessAreaDtos.add(businessAreaDtoEntry.getValue());
            }
        }
        return ResultMapHelper.success(businessAreaDtos,ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    public Set<Integer> findHotByAddr(String addrParentCity) {
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("addrCity",addrParentCity);
        conditionMap.put("isDelete",Constants.COMMON_FLAG_FALSE_INT);
        conditionMap.put("isHot",Constants.COMMON_FLAG_TRUE_INT);
        List<BmsBusinessArea> bmsBusinessAreas = bmsBusinessAreaDao.findAll(new SpecificationUtils(conditionMap));
        return bmsBusinessAreas.stream().map(BmsBusinessArea::getId).collect(Collectors.toSet());
    }

    @Override
    public void updateBmsInArea(Integer areaId) {
        Optional<BmsBusinessArea> bmsBusinessAreaOptional = bmsBusinessAreaDao.findById(areaId);
        if(bmsBusinessAreaOptional.isPresent()){
            BmsBusinessArea bmsBusinessArea = bmsBusinessAreaOptional.get();
            initBusinessCircle(bmsBusinessArea);
        }
    }

    public void initBusinessCircle(BmsBusinessArea bmsBusinessArea){
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("isUnload",Constants.COMMON_FLAG_FALSE_INT);
        conditionMap.put("isDelete",Constants.COMMON_FLAG_FALSE_INT);
        conditionMap.put("latitude_NN",Constants.COMMON_FLAG_TRUE_INT);
        conditionMap.put("longitude_NN",Constants.COMMON_FLAG_TRUE_INT);
        Set<Integer> isAuths = new HashSet<>();
        isAuths.add(1);
        isAuths.add(2);
        conditionMap.put("isAuth_IN",isAuths);
        List<BmsBusiness> bmsBusinesses = bmsBusinessService.getBaseDao().findAll(new SpecificationUtils(conditionMap));
        if(bmsBusinesses != null && bmsBusinesses.size() > 0){
            List<BmsBusinessAreaRelation> bmsBusinessAreaRelations = new LinkedList<>();
            for(BmsBusiness bmsBusiness : bmsBusinesses){
                String lat = bmsBusiness.getLatitude();
                String lnt = bmsBusiness.getLongitude();
                if(StringUtils.isNotBlank(lat) && StringUtils.isNotBlank(lnt)){
                    Point2D.Double point = new Point2D.Double(Double.parseDouble(lnt),Double.parseDouble(lat));
                    String content = bmsBusinessArea.getContent();
                    try {
                        if(StringUtils.isNotBlank(content)){
                            List<List> bs = JSON.parseArray(content,List.class);
                            if(bs != null && bs.size() > 0){
                                List<Point2D.Double> pointList = new LinkedList<>();
                                for(List event : bs){
                                    String pointLnt = event.get(0).toString();
                                    String pointLat = event.get(1).toString();
                                    if (StringUtils.isNotBlank(pointLnt) && StringUtils.isNotBlank(pointLat)) {
                                        Point2D.Double d = new Point2D.Double(Double.parseDouble(pointLnt), Double.parseDouble(pointLat));
                                        pointList.add(d);
                                    }
                                }
                                if(GeoUtils.IsPtInPoly(point,pointList)){
                                    //在商圈里面
                                    BmsBusinessAreaRelation businessAreaRelateion = bmsBusinessAreaRelationService.findByBusIdAndAreaId(bmsBusiness.getId(),bmsBusinessArea.getId());
                                    if(businessAreaRelateion == null){
                                        businessAreaRelateion = new BmsBusinessAreaRelation();
                                        businessAreaRelateion.setBusinessId(bmsBusiness.getId());
                                        businessAreaRelateion.setAreaId(bmsBusinessArea.getId());
                                        businessAreaRelateion.setNewTime(new Date());
                                        bmsBusinessAreaRelations.add(businessAreaRelateion);
                                    }
                                }
                            }
                        }
                    }catch (Exception e){
                        log.error(e.getMessage(),e);
                    }
                }
            }
            if(bmsBusinessAreaRelations.size() > 0){
                bmsBusinessAreaRelationService.getBaseDao().saveAll(bmsBusinessAreaRelations);
            }
        }
    }
}
